<?php

namespace App\Http\Controllers\Mq;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Library\Rabbitmq\Msg;
use Library\Rabbitmq\Simple;
use Library\Rabbitmq\Worker;
use Illuminate\Support\Facades\Log;
use Library\Rabbitmq\Publish;
use Library\Rabbitmq\Routing;

class Demo extends Controller
{
    public function route(Request $request)
    {
        $msg = new Msg();
        $msg->publish(['first_queue' => 'hello', 'time' => date('Y-m-d H:i:s')]);

        $msg = new Simple();
        $msg->publish(['first_queue' => 'hello simple queue', 'time' => date('Y-m-d H:i:s')]);
        return response('mq');

        $msg = new Simple();
        $msg->consumer(function ($msg) {
            //dump($msg->body);
            //echo ' [x] Received ', $msg->body, "\n";
            $message = json_decode($msg->body, true);
            Log::info($message);
            $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
        });
        return response('mq');

        $msg = new Worker();
        $msg->publish(['first_queue' => 'hello worker queue', 'time' => date('Y-m-d H:i:s')]);
        return response('mq');

        $msg = new Worker();
        $msg->consumer(function ($msg) {
            $message = json_decode($msg->body, true);
            Log::info($message);
            Log::info('pid--' . posix_getpid());
            $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
            sleep(2);
        });

        $msg = new Publish();
        $msg->publish(['first_queue' => 'publish queue', 'time' => date('Y-m-d H:i:s')]);
        return response('mq');

        $msg = new Publish();
        $msg->consumer(function ($msg) {
            $message = json_decode($msg->body, true);
            Log::info($message);
            Log::info('pid--' . posix_getpid());
            //$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
            sleep(5);
        });
        return response('mq');

        $msg = new Routing();
        $msg->routing_queue_create();
        $msg->publish(['first_queue' => 'Routing queue', 'time' => date('Y-m-d H:i:s')], '', 'info');
        $msg->publish(['first_queue' => 'Routing queue', 'time' => date('Y-m-d H:i:s')], '', 'error');
        return response('mq');

        $msg = new Routing('', 'routing_queue_1', true);
        $msg->consumer(function ($msg) {
            $message = json_decode($msg->body, true);
            Log::info($message);
            Log::info('pid--' . posix_getpid());
            //$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
            sleep(8);
        });
        return response('mq');

        $msg = new Routing('', 'routing_queue_2', true);
        $msg->consumer(function ($msg) {
            $message = json_decode($msg->body, true);
            Log::info($message);
            Log::info('pid--' . posix_getpid());
            //$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
            sleep(8);
        });
        return response('mq');
    }
}
